<?php
$loader->requireOnce('includes/PatientQueue/AbsQueue.class.php');
$loader->requireOnce('includes/PatientQueue/PatientQueueException.class.php');
$loader->requireOnce('ordo/Service.class.php');

class CashierQueue extends AbsQueue {
	
	private $queueType;
	
	private $queueId;
	
	function __construct($type) {
		$this->queueType = $type;
		
		$queue = Celini::newORDO('Queue');
		$id = $queue->getIdFromType($this->queueType);
		if ($id === false) {
			throw new PatientQueueException("Queue $type is not availabe in database");
		}
		
		$this->queueId = $id;
	}
	
	public function getType() {
		return $this->queueType;
	}
	
	/**
	 * Implement abstract function
	 *
	 */
	public function getQueueId() {
		return $this->queueId;
	}
	
	/**
	 * Implement abstract function
	 *
	 * @param int $roomId
	 */
	public function renderQueueAjax($roomId,$departmentId) {
		$queueItem = Celini::newORDO('QueueItem');
		$res = $queueItem->getAll($this->queueId, $roomId, $departmentId);
		$body = "";
		$count = 1;
		
		$ticketItem = Celini::newORDO('TicketItem');
		$config = Celini::configInstance();
		$averageTimePerPatient = $config->get('average_time_per_payment');
		foreach ($res as $row) {
			$limited = $averageTimePerPatient * $count;
			$waiting = $row['waiting_time'];
			$class = 'normal';
			if ($waiting >= $limited + 5) {
				$class = 'urgent';
			} else if ($waiting >= $limited) {
				$class = 'warning';
			}			
			$patientId = $row['patient_id'];
			
			$temp = $ticketItem->serviceNameAndPaymentType($row['ticket_item_id']);
			$serviceName = $temp['name'];			
			$paymentType = $temp['payment_type'];
			if ($paymentType == Service::AFTER) {
				$paymentType = L10nHelper::_("All");
			} else if ($paymentType == Service::BEFORE) {
				$paymentType = L10nHelper::_("Prepaid");
			}
			
			$aRow = "<tr id=\"" . $row['ticket_item_id'] . "\">";
			$aRow .= "<td>$count</td>";
			
			$url = Celini::link('viewInprogressItem', 'Cashier') . "ticketItemId=" . $row['ticket_item_id'];
			$aRow .= "<td><a href=\"javascript:preCheckAvailable(" . $row['ticket_item_id'] . ", '$url');\" title=\"".L10nHelper::_("Collect fee")."\">". $row['patient_name'] . "</a></td>";						
			$aRow .= "<td>" . $row['patient_code'] . "</td>";
			$aRow .= "<td> $serviceName </td>";		
			$aRow .= "<td> $paymentType </td>";	
			$aRow .= "<td class=\"$class\">" . L10nHelper::_("%s/%s mins", $waiting, $limited). "</td>";			
			$aRow .= "<td><a href=\"" .Celini::link('viewFrame', 'Cashier') . "ticketItemId=" . $row['ticket_item_id'] ."\" title=\"".L10nHelper::_("View service ticket, prescription")."\">".L10nHelper::_("View profile")."</a></td>";			
			$aRow .= "</tr>";
			
			$count++;
			$body .= $aRow;			
		}
		$total = '<table cellspacing="0" cellpadding="0" style="width: 100%;">';
		$total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
		$total .= '</table>';
		if ($body == '') {
			$body = '<tr class="last"><td colspan="7">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		return array('body' => $body, 'total_table' => $total);
	}
	
	public function renderQuickQueueAjax($roomId, $itemPerPage, $page, $holderId = 0) {

	}
	
	///////////////////// PRIVATE METHODS ///////////
}
?>